import 'package:flutter/material.dart';
import 'package:flutter_dva_hooks/flutter_dva_hooks.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:todo_app/components/Overlay/main.dart';
import 'package:todo_app/components/TextInput/CustomTextInput.dart';
import 'package:todo_app/constants/DColor.dart';
import 'package:todo_app/constants/Screen.dart';
import 'package:todo_app/layout/Gaps.dart';
import 'package:todo_app/layout/Space.dart';
import 'package:todo_app/models/getters.dart';
import 'package:todo_app/utils/mqtt/MessageEventBus.dart';

class MessagePublishPanel extends HookWidget {
  const MessagePublishPanel({Key? key, required this.category, required this.receivedId }) : super(key: key);
  final MessageCategory category;
  final int receivedId;

  @override
  Widget build(BuildContext context) {
    var openPanel = useState(false);
    var controller = useTextEditingController(text: "");
    var store = useDvaStore();
    var meUid = store.rootState.system?.user?.id;

    return AnimatedSize(
      duration: const Duration(milliseconds: 350),
      child: Container(
        height: openPanel.value == true ? 360.dp : 56.dp,
        width: Screen.width,
        color: Colors.white,
        child: Space(
          gutter: 12.dp,
          children: [
            Expanded(
              child: CustomTextInput(
                controller: controller,
                height: 32.dp,
                padding: EdgeInsets.symmetric(
                  horizontal: 6.dp,
                  vertical: 3.dp
                ),
                decoration: const BoxDecoration(
                  color: DColor.bgColor
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                if(controller.text.isEmpty) {
                  showOverlayToast("请输入要发送的聊天内容");
                  return;
                }
                var content = controller.text;
                controller.text = "";
                MessageEventBus.sendTextMessage(meUid!, category, receivedId, content);
              },
              child: const Icon(Icons.send, color: DColor.primary),
            )
          ],
        ),
      ),
    );
  }
}